import time
import hashlib
from flask import Flask, jsonify, request
from flask_caching import Cache
from config.dev import *
from en_wechat_handler import *

app = Flask(__name__)

# 配置缓存
app.config['CACHE_TYPE'] = 'redis'
app.config['CACHE_REDIS_URL'] = 'redis://120.76.119.112:6379/0'  # 根据实际情况配置 Redis 连接信息
app.config['CACHE_KEY_PREFIX'] = 'flask_cache_enwechat:'

# 初始化缓存扩展
cache = Cache(app)


@app.route('/')
def hello_world():
    return '企业微信'


@app.route('/api/enwechat/config', methods=['GET'])
def get_en_wechat_config():
    jsapi_ticket = fetch_en_jsapi_ticket(CROPID=EN_CROPID, SECERT=EN_H5_SECERT)
    nonceStr = 'dsafasfdsfdsaf'
    timestamp = str(int(time.time()))
    url = request.args.get('url')
    print(url)

    sign_str = f'jsapi_ticket={jsapi_ticket}&noncestr={nonceStr}&timestamp={timestamp}&url={url}'
    signature = hashlib.sha1(sign_str.encode('utf-8')).hexdigest()

    return {
        'jsapi_ticket': jsapi_ticket,
        'appId': EN_CROPID,
        'nonceStr': nonceStr,
        'timestamp': timestamp,
        'url': url,
        'signature': signature
        }


@app.route('/api/enwechat/agentconfig', methods=['GET'])
def get_en_wechat_agentConfig():
    jsapi_ticket = fetch_app_jsapi_ticket(CROPID=EN_CROPID, SECERT=EN_H5_SECERT)
    nonceStr = 'dsafasfdsfdsaf'
    timestamp = str(int(time.time()))
    url = request.args.get('url')
    print(url)

    sign_str = f'jsapi_ticket={jsapi_ticket}&noncestr={nonceStr}&timestamp={timestamp}&url={url}'

    signature = hashlib.sha1(sign_str.encode('utf-8')).hexdigest()

    return {
        'jsapi_ticket': jsapi_ticket,
        'appId': EN_CROPID,
        'agentid': EV_h5_AGENTID,
        'nonceStr': nonceStr,
        'timestamp': timestamp,
        'url': url,
        'signature': signature
        }


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=3000)